﻿<!--/* 
* Copyright (c) 2009, The University of Texas at Austin
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, 
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, 
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, 
* this list of conditions and the following disclaimer in the documentation and/or other materials 
* provided with the distribution.
*
* Neither the name of The University of Texas at Austin nor the names of its contributors may be 
* used to endorse or promote products derived from this software without specific prior written 
* permission.
* 
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/-->
<Window x:Class="BioBrowser.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:julmar="http://www.julmar.com/wpfhelpers"
    xmlns:ViewModels="clr-namespace:BioBrowser.ViewModels"
    xmlns:Interactivity="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 
    xmlns:SharedResources="clr-namespace:Bio.SharedResources;assembly=Bio.SharedResources" xmlns:ShaderEffectLibrary="clr-namespace:ShaderEffectLibrary;assembly=ShaderEffectLibrary" 
    Title="CATUI - The Comparative Analysis Toolkit User Interface" MinWidth="800" MinHeight="600"
    WindowStartupLocation="CenterScreen" ResizeMode="CanResize" Background="#FF535353" julmar:HelpProvider.Filename="CATUI.chm"
    DataContext="{julmar:ViewModelCreator ViewModelType={x:Type ViewModels:MainViewModel}}">
    
    <Window.InputBindings>
        <KeyBinding Command="{Binding OpenDataFile}" Key="O" Modifiers="Control" />
        <KeyBinding Command="{Binding CloseCurrentView}" Key="V" Modifiers="Alt" />
    </Window.InputBindings>
        
	<Window.Resources>
        
		<Style x:Key="DockedTabStyle" TargetType="{x:Type TabItem}" BasedOn="{StaticResource {x:Type TabItem}}">
			<Setter Property="HeaderTemplate">
				<Setter.Value>
					<DataTemplate>
						<Grid HorizontalAlignment="Left" VerticalAlignment="Center">
							<Grid.ColumnDefinitions>
								<ColumnDefinition Width="Auto" />
								<ColumnDefinition />
								<ColumnDefinition Width="Auto" />
							</Grid.ColumnDefinitions>

							<ToggleButton Grid.Column="0" Margin="2,2,5,0" Style="{StaticResource TogglePin}" IsChecked="{Binding IsDocked}" />
							<Label Grid.Column="1" Content="{Binding Title}" VerticalAlignment="Center" HorizontalAlignment="Center" />
							<Button Grid.Column="2" Command="{Binding CloseCommand}" Margin="5,2,2,0" Style="{StaticResource CloseButton}" />
						</Grid>
					</DataTemplate>
				</Setter.Value>
			</Setter>
		</Style>

        <Style x:Key="TopLevelMenuStyle" TargetType="MenuItem" BasedOn="{StaticResource {x:Type MenuItem}}">
            <Setter Property="Header" Value="{Binding Text}" />
            <Setter Property="ItemsSource" Value="{Binding Children}" />
            <Setter Property="Command" Value="{Binding Command}" />
            <Setter Property="InputGestureText" Value="{Binding GestureText}" />
            <Setter Property="IsChecked" Value="{Binding IsChecked, Mode=OneWay}" />
        </Style>

        <Style x:Key="WindowMenuStyle" TargetType="MenuItem" BasedOn="{StaticResource {x:Type MenuItem}}">
            <Setter Property="Header" Value="{Binding Title}" />
            <Setter Property="Command" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Menu}}, Path=DataContext.ActivateView}" />
            <Setter Property="CommandParameter" Value="{Binding}" />
        </Style>
        
        <Style x:Key="ContextMenuItemStyle" TargetType="MenuItem" BasedOn="{StaticResource {x:Type MenuItem}}">
            <Setter Property="Header" Value="{Binding Text}"/>
            <!--<Setter Property="Icon" Value="{Binding Icon}" />-->
            <Setter Property="ItemsSource" Value="{Binding Children}"/>
            <Setter Property="Command" Value="{Binding Command}" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding Text}" Value="{x:Null}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="MenuItem">
                                <Separator />
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>

        <HierarchicalDataTemplate DataType="{x:Type ViewModels:OpenBioDataViewModel}" ItemsSource="{Binding Children}">
            <StackPanel Orientation="Horizontal" Background="Transparent">
                <Interactivity:Interaction.Triggers>
                    <julmar:DoubleClickTrigger>
                        <julmar:InvokeCommand Command="{Binding DefaultCommand}" />
                    </julmar:DoubleClickTrigger>
                </Interactivity:Interaction.Triggers>

                <StackPanel.ContextMenu>
                    <ContextMenu ItemContainerStyle="{StaticResource ContextMenuItemStyle}" ItemsSource="{Binding MenuOptions}"  />
                </StackPanel.ContextMenu>

                <Ellipse x:Name="loadingBox" StrokeThickness="2" StrokeDashArray=".5,1" Width="24" Height="24" RenderTransformOrigin="0.5,0.5" CacheMode="BitmapCache">
                    <Ellipse.Triggers>
                        <EventTrigger RoutedEvent="Ellipse.Loaded">
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)" RepeatBehavior="Forever">
                                        <SplineDoubleKeyFrame KeyTime="00:00:02" Value="360"/>
                                    </DoubleAnimationUsingKeyFrames>
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                    </Ellipse.Triggers>
                    <Ellipse.RenderTransform>
                        <RotateTransform/>
                    </Ellipse.RenderTransform>
                    <Ellipse.Stroke>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="White" Offset="0"/>
                            <GradientStop Color="#FFC9C9C9" Offset="0.5"/>
                            <GradientStop Color="Black" Offset="1"/>
                        </LinearGradientBrush>
                    </Ellipse.Stroke>
                </Ellipse>
                <Image x:Name="loadedImage" Visibility="Collapsed" Source="{Binding Image}" Width="24" Height="24" />
                <Label x:Name="tb" Margin="5" HorizontalAlignment="Left" VerticalAlignment="Center" Content="{Binding Header}"  />
            </StackPanel>
            <DataTemplate.Triggers>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding IsLoading}" Value="False" />
                        <Condition Binding="{Binding IsLoaded}" Value="False" />
                    </MultiDataTrigger.Conditions>
                    <MultiDataTrigger.Setters>
                        <Setter TargetName="loadingBox" Property="Visibility" Value="Collapsed" />
                        <Setter TargetName="loadedImage" Property="Visibility" Value="Visible" />
                        <Setter TargetName="loadedImage" Property="Effect">
                            <Setter.Value>
                                <ShaderEffectLibrary:MonochromeEffect />
                            </Setter.Value>
                        </Setter>
                    </MultiDataTrigger.Setters>
                </MultiDataTrigger>
                <DataTrigger Binding="{Binding IsLoaded}" Value="True">
                    <Setter TargetName="loadingBox" Property="Visibility" Value="Collapsed" />
                    <Setter TargetName="loadedImage" Property="Visibility" Value="Visible" />
                </DataTrigger>
                <DataTrigger Binding="{Binding IsLoaded}" Value="False">
                    <Setter TargetName="tb" Property="Foreground" Value="LightGray" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </HierarchicalDataTemplate>

        <HierarchicalDataTemplate DataType="{x:Type ViewModels:WorkspaceViewModel}" ItemsSource="{Binding AvailableDataSources}">
            <StackPanel Orientation="Horizontal">
                <Image Source="{Binding Image}" Width="24" Height="24" />
                <Label Margin="5" HorizontalAlignment="Left" VerticalAlignment="Center" Content="{Binding Header}" />
                <StackPanel.ContextMenu>
                    <ContextMenu ItemContainerStyle="{StaticResource ContextMenuItemStyle}" ItemsSource="{Binding MenuOptions}"  />
                </StackPanel.ContextMenu>
            </StackPanel>
        </HierarchicalDataTemplate>

        <DataTemplate DataType="{x:Type ViewModels:OpenBioViewModel}">
            <StackPanel Orientation="Horizontal" Background="Transparent">
                <Interactivity:Interaction.Triggers>
                    <julmar:DoubleClickTrigger>
                        <julmar:InvokeCommand Command="{Binding ActivateCommand}" />
                    </julmar:DoubleClickTrigger>
                </Interactivity:Interaction.Triggers>
                <Image Source="{Binding Image}" Width="24" Height="24" />
                <Label Margin="5" HorizontalAlignment="Left" VerticalAlignment="Center" Content="{Binding Header}" />
                <StackPanel.ContextMenu>
                    <ContextMenu ItemContainerStyle="{StaticResource ContextMenuItemStyle}" ItemsSource="{Binding MenuOptions}"  />
                </StackPanel.ContextMenu>
            </StackPanel>
        </DataTemplate>

        <julmar:IntegerToVisibilityConverter x:Key="CountToVisibilityConverter" ZeroTreatment="Collapsed" NonzeroTreatment="Visible" />
        
    </Window.Resources>

    <Grid>

        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition />
        </Grid.RowDefinitions>
        
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <!-- Main Menu -->
        <Menu Grid.Row="0" Grid.Column="2">
            <MenuItem Header="_File">
                <MenuItem Header="Create _New Workspace" Command="{Binding CreateNewWorkspace}" />
                <MenuItem Header="_Open" ItemsSource="{Binding OpenFileMenu}" ItemContainerStyle="{StaticResource TopLevelMenuStyle}" />
                <MenuItem Header="_Close View" Command="{Binding CloseCurrentView}" InputGestureText="Alt+V" />
                <MenuItem Header="E_xit" Command="{Binding CloseApplication}" InputGestureText="Alt+F4" />
            </MenuItem>
            <MenuItem Header="E_dit" ItemsSource="{Binding EditMenu}" ItemContainerStyle="{StaticResource TopLevelMenuStyle}" 
                      Visibility="{Binding EditMenu.Count, Converter={StaticResource CountToVisibilityConverter}, FallbackValue=Collapsed}" />
            <MenuItem Header="_Actions" ItemsSource="{Binding ActionsMenu}" ItemContainerStyle="{StaticResource TopLevelMenuStyle}"
                      Visibility="{Binding ActionsMenu.Count, Converter={StaticResource CountToVisibilityConverter}, FallbackValue=Collapsed}" />
            <MenuItem Header="_Options" ItemsSource="{Binding OptionsMenu}" ItemContainerStyle="{StaticResource TopLevelMenuStyle}" 
                      Visibility="{Binding OptionsMenu.Count, Converter={StaticResource CountToVisibilityConverter}, FallbackValue=Collapsed}" />
            <MenuItem Header="_View" ItemsSource="{Binding ViewMenu}" ItemContainerStyle="{StaticResource TopLevelMenuStyle}" 
                      Visibility="{Binding ViewMenu.Count, Converter={StaticResource CountToVisibilityConverter}, FallbackValue=Collapsed}" />
            <MenuItem Header="_Windows" ItemsSource="{Binding AllViews}" ItemContainerStyle="{StaticResource WindowMenuStyle}" 
                      Visibility="{Binding AllViews.Count, Converter={StaticResource CountToVisibilityConverter}, FallbackValue=Collapsed}" />
            <MenuItem DockPanel.Dock="Right" HorizontalContentAlignment="Right" Margin="0,0,-10,0">
                <MenuItem.Header>
                    <Grid ToolTip="Help">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>
                        <Ellipse Width="20" Height="20" Stroke="White" StrokeThickness="1" Fill="Black" />
                        <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="?" />
                        <Label Content="Help" Grid.Column="1" Margin="10,0,0,0" VerticalAlignment="Center" />
                    </Grid>
                </MenuItem.Header>
                <MenuItem Header="Help" Command="ApplicationCommands.Help" />
                <MenuItem Header="A_bout CATUI..." Click="OnShowAboutBox" />
            </MenuItem>
        </Menu>

        <!-- Sidebar -->
        <Grid Grid.Column="0" Grid.RowSpan="2" >

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>

            <Expander IsExpanded="True" ExpandDirection="Right">
                <SharedResources:ProportionalPanel x:Name="sideBar" HorizontalAlignment="Stretch" Width="300">

                    <Expander IsExpanded="True">
                        <Expander.Header>
                            <TextBlock Text="Available Data Sources" Margin="5,0" />
                        </Expander.Header>
                        <TreeView ItemsSource="{Binding OpenDataSources}" BorderBrush="{x:Null}">
                            <TreeView.ItemContainerStyle>
                                <Style TargetType="TreeViewItem" BasedOn="{StaticResource {x:Type TreeViewItem}}">
                                    <Setter Property="IsExpanded" Value="{Binding IsExpanded}" />
                                    <Setter Property="IsSelected" Value="{Binding IsSelected}" />
                                </Style>
                            </TreeView.ItemContainerStyle>
                        </TreeView>
                    </Expander>

                    <ItemsControl ItemsSource="{Binding SidebarItems}" Grid.Row="1">
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <SharedResources:ProportionalPanel />
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition />
                                        <ColumnDefinition Width="Auto" />
                                        <ColumnDefinition Width="Auto" />
                                    </Grid.ColumnDefinitions>
                                    <Expander Header="{Binding Title}" IsExpanded="True" Grid.ColumnSpan="3">
                                        <ContentControl Content="{Binding Visual}" VerticalAlignment="Top" />
                                    </Expander>
                                    <ToggleButton Grid.Column="1" VerticalAlignment="Top" Style="{DynamicResource TogglePin}" IsChecked="{Binding Visual.DataContext.IsDocked}" Margin="4" />
                                    <Button Grid.Column="2" VerticalAlignment="Top" Style="{DynamicResource CloseButton}" Command="{Binding Visual.DataContext.CloseView}" Margin="4" />
                                </Grid>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </SharedResources:ProportionalPanel>
            </Expander>        
        
            <Thumb Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Stretch" DragDelta="OnResizeSideBarDelta" Width="2" Background="Black" Focusable="True" Cursor="SizeWE" />
        </Grid>

        <!-- Main content -->
        <TabControl Grid.Column="1" Grid.Row="1" ItemsSource="{Binding DockedViews}"
                    SelectedItem="{Binding SelectedView}" ItemContainerStyle="{DynamicResource DockedTabStyle}">
            
            <TabControl.ContentTemplate>
                <DataTemplate>
                    <ContentControl Content="{Binding Visual}"  HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
                </DataTemplate>
            </TabControl.ContentTemplate>
        </TabControl>

    </Grid>

</Window>
